<!-- 优享专区首页 -->
<template>
	<view class="content">
		<view class="bigBox">
			<view class="header">
				<text class="cell-more yticon icon-zuo" @tap="navBack"></text>
				<view class="uni-searchbox">
					<view class="uni-search-input">
						<icon type="search" :size='13' color='#333'></icon>
						<!-- <input type="text" confirm-type="search" placeholder="请输入商品名字" placeholder-class="uni-input-plholder" class="uni-input" v-model.trim="key" @confirm="confirmKey" /> -->

						<input type="text" confirm-type="search" placeholder="请输入关键字搜索" placeholder-class="uni-input-plholder" class="uni-input"
						 v-model.trim="keys" @confirm="searchSubmit" />

						<!-- @input="inputKey" -->
						<!-- <icon type="clear" :size='13' color='#bcbcbc' @tap="cleanKey" v-show="keys"></icon> -->
					</view>
				</view>
				<text></text>
			</view>
			<view class="navbar">
				<view class="nav-item" :class="{current: filterIndex === 0}" @click="tabClick(0)">
					默认
				</view>
				<view class="nav-item" :class="{current: filterIndex === 1}" @click="tabClick(1)">
					销量
				</view>
				<view class="nav-item" :class="{current: filterIndex === 2}" @click="tabClick(2)">
					<text>价格</text>
					<view class="p-box">
						<text :class="{active: priceOrder === 1 && filterIndex === 2}" class="yticon icon-shang"></text>
						<text :class="{active: priceOrder === 2 && filterIndex === 2}" class="yticon icon-shang xia"></text>
					</view>
				</view>
				<!-- <text class="cate-item yticon icon-fenlei1" @click="toggleCateMask('show')"></text> -->
			</view>
		</view>

		<view class="goods-list">
			<empty v-if="emptyShow" :custom="true" src="/mall/static/images/no-goods.png" text="暂无搜索结果"></empty>
			<goods-block :goodsList="goodsList" :scrollTop="scrollTop" :enjoy="true"></goods-block>
			<uni-load-more :status="loadingType"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		systemConfig
	} from '@/api/public.js'
	import {
		getProducts
	} from '@/api/mall.js'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	import goodsBlock from "@/components/goodsBlock/goodsBlock.vue"
	export default {
		components: {
			uniLoadMore,
			goodsBlock
		},
		data() {
			return {
				emptyShow: false,
				cateMaskState: 0, //分类面板展开状态
				headerPosition: "fixed",
				headerTop: "200rpx",
				loadingType: 'more', //加载更多状态
				filterIndex: 0,
				cid: '',
				cateId: '', //已选三级分类id
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				salesOrder: 0,
				cateList: [],
				goodsList: [],
				page: 1,
				limit: 14,
				keys: '',
				personal_shell_ration: 1,
				seller_id: '',
				priceRange: '',
				priceType: '',
				scrollTop: 0
			};
		},
		created() {
			systemConfig({
				filed: 'personal_shell_ration'
			}).then(res => {
				this.personal_shell_ration = Number(res.msg) / 100;
			})
		},
		onLoad(options) {
			// if(options.cid){
			// 	this.cid = options.cid;
			// }
			if (options.id) {
				this.cateId = options.id;
			}
			if (options.name) {
				uni.setNavigationBarTitle({
					title: options.name
				});
			}
			if (options.key) {
				this.keys = options.key;
			}
			if (options.seller_id) {
				this.seller_id = options.seller_id;
			}
			if (options.priceRange) {
				this.priceRange = options.priceRange;
			}
			if (options.priceType) {
				this.priceType = options.priceType;
			}
			this.loadData();
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			 this.scrollTop = e.scrollTop;
			if (e.scrollTop >= 0) {
				this.headerPosition = "fixed";
			} else {
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			this.loadData('refresh');
		},
		//加载更多
		onReachBottom() {
			this.page = this.page + 1;
			this.loadData();
		},
		methods: {
			searchSubmit() {
				this.loadingType = 'more'
				this.page = 1;
				this.goodsList = [];
				this.loadData();
			},
			cleanKey() {
				this.loadingType = 'more'
				this.page = 1;
				this.keys = "";
				this.goodsList = [];
				this.loadData();
			},
			//加载商品 ，带下拉刷新和上滑加载
			loadData(type = 'add', loading) {
				//没有更多直接返回
				if (type === 'add') {
					if (this.loadingType === 'nomore') {
						return;
					}
					this.loadingType = 'loading';
				} else {
					this.loadingType = 'more'
				}
				getProducts({
					cid: this.cid || "",
					sid: this.cateId || "",
					page: this.page,
					limit: this.limit,
					keyword: this.keys,
					priceOrder: this.priceOrder, //价格排序
					salesOrder: this.salesOrder, //销量排序
					seller_id: this.seller_id || "", //商户Id
					priceRange: this.priceRange,
					priceType: this.priceType,
					product_type: 1
				}).then(res => {
					let list = res.data.map((val, key) => {
						val.blackS = (Number(val.price) * ((100 - val.rebate_ratio) * this.personal_shell_ration) / 100).toFixed(2);
						return val;
					})
					this.goodsList = this.goodsList.concat(list);
					this.loadingType = res.data.length < this.limit ? 'nomore' : 'more';
					if(!this.goodsList.length) {
						this.emptyShow = true
					}

				})
				if (type === 'refresh') {
					this.goodsList = [];
				}
				//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
				if (type === 'refresh') {
					if (loading == 1) {
						uni.hideLoading()
					} else {
						uni.stopPullDownRefresh();
					}
				}
			},
			//筛选点击
			tabClick(index) {
				if (this.filterIndex === index && index !== 2) {
					return;
				}
				this.page = 1;
				this.goodsList = [];
				this.emptyShow = false
				this.filterIndex = index;

				if (index === 2) {
					this.salesOrder = 0;
					this.priceOrder = this.priceOrder === 1 ? 'desc' : 1;
				} else {
					this.priceOrder = 0;
					this.salesOrder = 0;
				}
				if (index === 1) {
					this.salesOrder = 'desc';
					this.priceOrder = 0;
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					mask:true,
					title: '正在加载'
				})
			},
			stopPrevent() {},
			navBack() {
				uni.navigateBack({

				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	page,
	.content {
		background: $page-color-base;
		min-height: 100vh;
		box-sizing: border-box;
	}

	.bigBox {
		position: fixed;
		top: 0;
		width: 100%;
		//#ifndef H5
		height: 304rpx;
		//#endif
		//#ifdef H5
		height: 250rpx;
		//#endif
		z-index: 99;
	}

	.content {

		// padding-top: 180rpx;
		.header {

			width: 100%;
			display: flex;
			align-items: center;
			background-color: #fff;
			//#ifndef H5
			padding: 96rpx 30rpx 40rpx;
			//#endif
			//#ifdef H5
			padding: 40rpx 30rpx 40rpx;

			//#endif
			.icon-zuo {
				font-size: 40rpx;
			}

			.uni-searchbox {
				box-sizing: border-box;
				display: flex;
				align-items: center;
				width: 55%;
				margin-left: 30rpx;

				.uni-search-input {
					width: 100%;
					height: 66rpx;
					border-radius: 35rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					background: #eeeeee;
					display: flex;
					align-items: center;
					flex-wrap: nowrap;
				}

				.uni-input {
					flex: 1;
					color: #333;
					padding: 0 16rpx;
					font-size: 28rpx;
				}

				.uni-input-plholder {
					font-size: 28rpx;
					color: #b2b2b2;
				}
			}
		}

		.navbar {
			// position: fixed;
			// left: 0;
			// top: 200rpx;
			display: flex;
			width: 100%;
			height: 80rpx;
			background: #fff;
			box-shadow: 0 10rpx 10rpx rgba(0, 0, 0, .04);
			z-index: 10;

			.nav-item {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				font-size: 30rpx;
				color: $font-color-dark;
				position: relative;

				&.current {
					color: $mall-price;

					&:after {
						content: '';
						position: absolute;
						left: 50%;
						bottom: 0;
						transform: translateX(-50%);
						width: 120rpx;
						height: 0;
						border-bottom: 4rpx solid $mall-price;
					}
				}
			}

			.p-box {
				display: flex;
				flex-direction: column;

				.yticon {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 30rpx;
					height: 14rpx;
					line-height: 1;
					margin-left: 4rpx;
					font-size: 26rpx;
					color: #888;

					&.active {
						color: $mall-color-pri;
					}
				}

				.xia {
					transform: scaleY(-1);
				}
			}

			.cate-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				width: 80rpx;
				position: relative;
				font-size: 44rpx;

				&:after {
					content: '';
					position: absolute;
					left: 0;
					top: 50%;
					transform: translateY(-50%);
					border-left: 1px solid #ddd;
					width: 0;
					height: 36rpx;
				}
			}
		}

		/* 分类 */
		.cate-mask {
			position: fixed;
			left: 0;
			top: var(--window-top);
			bottom: 0;
			width: 100%;
			background: rgba(0, 0, 0, 0);
			z-index: 95;
			transition: .3s;

			.cate-content {
				width: 630rpx;
				height: 100%;
				background: #fff;
				float: right;
				transform: translateX(100%);
				transition: .3s;
			}

			&.none {
				display: none;
			}

			&.show {
				background: rgba(0, 0, 0, .4);

				.cate-content {
					transform: translateX(0);
				}
			}
		}

		.cate-list {
			display: flex;
			flex-direction: column;
			height: 100%;

			.cate-item {
				display: flex;
				align-items: center;
				height: 90rpx;
				padding-left: 30rpx;
				font-size: 28rpx;
				color: #555;
				position: relative;
			}

			.two {
				height: 64rpx;
				color: #303133;
				font-size: 30rpx;
				background: #f8f8f8;
			}

			.active {
				color: $mall-price;
			}
		}

		.empty__class {
			position: relative;
			z-index: 0;
		}

		/* 商品列表 */
		.goods-list {
			//#ifndef H5
			padding: 310rpx 30rpx 0;
			//#endif
			//#ifdef H5
			padding: 260rpx 30rpx 0;
			//#endif
		}
	}
</style>
